import com.mysql.cj.jdbc.MysqlDataSource;

import javax.naming.ldap.PagedResultsControl;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class JDBC {
    public static void main(String[] args) throws SQLException {
        //使用Datasource来描述MySQL服务器的位置
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java104?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");


        //建立连接
        Connection connection = dataSource.getConnection();
        //System.out.println(connection);

        //构建sql语句
        String sql = "insert into student values(1,'温习文')";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        //执行sql语句
        int n = preparedStatement.executeUpdate();
        System.out.println(n);

        //断开连接,释放资源
        preparedStatement.close();
        connection.close();
    }
    public static void main1(String[] args) {
        //第一种写法
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java104?characterEncoding=utf8&useSSL=false");

        //第二种写法
        MysqlDataSource dataSource1 = new MysqlDataSource();
        dataSource1.setUrl("jdbc:mysql://127.0.0.1:3306/java104?characterEncoding=utf8&useSSL=false");
    }
}
